#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/12/17 上午10:57
# @Author  : fugang_le
# @Software: PyCharm

import os
import sys
import re
import json
import random
import pandas as pd
import fasttext
import jieba
import logging

from src.config import STOP_WORD_PATH, ROOTPATH
from src.data_helper import load_data

logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

train_data_path = os.path.join(ROOTPATH, 'data/fasttext_train.csv')
test_data_path = os.path.join(ROOTPATH, 'data/fasttext_test.csv')
model_path = os.path.join(ROOTPATH, "model/fasttext.model")
load_model_path = os.path.join(ROOTPATH, 'model/fasttext.model.bin')
vec_file_path = os.path.join(ROOTPATH, 'model/cc.zh.300.vec')


data, domain = load_data()
stop_word = load_stop_word()
model = None

'''
fasttext.load_model(load_model_path, label_prefix='__label__')
'''
def train():
    data_preprocessing()
    # 训练模型
    logging.info('training  ......')
    classifier = fasttext.supervised(train_data_path,
                                     model_path,
                                     label_prefix="__label__",
                                     dim=300,
                                     silent=0,)
    '''
    input_file                 训练文件路径（必须）
    output                     输出文件路径（必须）
    label_prefix               标签前缀 default __label__
    lr                         学习率 default 0.1
    lr_update_rate             学习率更新速率 default 100
    dim                        词向量维度 default 100
    ws                         上下文窗口大小 default 5
    epoch                      epochs 数量 default 5
    min_count                  最低词频 default 5
    word_ngrams                n-gram 设置 default 1
    loss                       损失函数 {ns,hs,softmax} default softmax
    minn                       最小字符长度 default 0
    maxn                       最大字符长度 default 0
    thread                     线程数量 default 12
    t                          采样阈值 default 0.0001
    silent                     禁用 c++ 扩展日志输出 default 1
    encoding                   指定 input_file 编码 default utf-8
    pretrained_vectors         指定使用已有的词向量 .vec 文件 default None
    '''
    result = classifier.test(test_data_path)
    print("precision rate: ", result.precision)
    print("recall rate: ", result.recall)



if __name__ == '__main__':
    train()

